const mysql = require('mysql');

const pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: 'czq123456',
    database: 'demo',
    connectionLimit: 10
})

// pool.query('select * from emp', (err, result, fields) => {
//     if (err) throw err;
//     // console.log(result)
// })
//
// pool.getConnection((err, connection) => {
//     if (err) throw err;
//     connection.query('select * from emp', (err, result) => {
//         if (err) throw err;
//         // console.log(result);
//         connection.release();
//     })
// })

// promise 控制对象
module.exports = (sql, params) => {
    return new Promise((resolve, reject) => {
        pool.getConnection((err, connection) => {
            if (err) throw err;
            connection.query(sql, params, (err, result, fields) => {
                connection.release(); // 释放连接
                if (err) reject(err);
                resolve(result)
            })
        })
    })
}